﻿using System;

namespace Kaleida.ServiceMonitor.Framework.Responses
{
    public class PingResponse : ITimedEvent, IStringContainer
    {
        private readonly TimeSpan timeTaken;
        private readonly Exception exception;

        public PingResponse(TimeSpan timeTaken)
        {
            this.timeTaken = timeTaken;
        }

        public PingResponse(Exception exception)
        {
            this.exception = exception;
        }

        public bool Success
        {
            get { return exception == null; }
        }

        public Exception Exception
        {
            get
            {
                if (Success)
                    throw new InvalidOperationException("Ping completed successfully");

                return exception;
            }
        }

        public override string ToString()
        {
            if (Success)
                return string.Format("Ping completed with status Success in {0}ms", timeTaken.TotalMilliseconds);

            return "An Exception occured: " + exception.Message;
        }

        public TimeSpan TimeTaken
        {
            get
            {
                if (!Success)
                    throw new InvalidOperationException("An Exception occured: " + exception.Message);

                return timeTaken;
            }
        }
        
        public string Content
        {
			get { return Success ? "Success" : ToString(); }
        }
    }
}
